# Maintainer: Carlos Antunes <cmantunes@gmail.com>
# Contributed by: nanodude

_realname=gdb
_target=arm-none-eabi

pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_target}-${_realname}")
pkgver=7.6.0
pkgrel=1
pkgdesc="GNU Tools for ARM Embedded Processors - GDB (mingw-w64)"
arch=('any')
url="https://launchpad.net/gcc-arm-embedded"
license=('GPL' 'Various')
groups=("${MINGW_PACKAGE_PREFIX}-${_target}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' '!debug' 'strip')
source=('gcc-arm-embedded.tar.bz2::https://launchpad.net/gcc-arm-embedded/4.8/4.8-2014-q3-update/+download/gcc-arm-none-eabi-4_8-2014q3-20140805-src.tar.bz2'
        '10-remove-duplicate-tputs.patch')
sha256sums=('09519946642d5366ab739a89d3388b2ea333cd7dc50192bab3736a9593af83e6'
            '80c719e0d91b4c712e03a2dd7f2d5197a16d32465d4514d904f4c910cfddb997')
noextract=('gcc-arm-embedded.tar.bz2')

prepare() {
  # Extract packages from archive
  rm -rf ${srcdir}/tmp
  mkdir ${srcdir}/tmp && cd ${srcdir}/tmp
  tar  xvf ../../gcc-arm-embedded.tar.bz2 --strip-components=2

  # Copy and extract GDB package
  cp gdb.tar.bz2 ${srcdir}
  cd ${srcdir}
  tar xvf gdb.tar.bz2

  # GDB: remove duplicate tputs
  patch -p1 -i "${srcdir}/10-remove-duplicate-tputs.patch"
}

build() {
  # Build GDB
  cd $srcdir
  rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  ../${_realname}/configure \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${_target}" \
    --prefix="${MINGW_PREFIX}" \
    --disable-werror \
    --disable-nls \
    --with-system-readline
  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make -j1 DESTDIR=${pkgdir} install

  # Remove files that conflict with host
  rm -rf "${pkgdir}${MINGW_PREFIX}/share/info"
  rm -rf "${pkgdir}${MINGW_PREFIX}/share/gdb"
  rm -rf "${pkgdir}${MINGW_PREFIX}/include/gdb/jit-reader.h"
  rm -rf "${pkgdir}${MINGW_PREFIX}/lib/libiberty.a"
}

